#include "bits/stdc++.h"
#define For(i,a,b) for(int i=a;i<=b;i++)
#define Rev(i,a,b) for(int i=a;i>=b;i--)
#define Fin(file) freopen(file,"r",stdin)
#define Fout(file) freopen(file,"w",stdout)
#define assume(expr) ((!!(expr))||(exit((fprintf(stderr,"Assumption Failed: %s on Line %d\n",#expr,__LINE__),-1)),false))
using namespace std;
const int N=8e3+5,M=6e4+5; typedef long long ll;
int np[M],O[M],n,m,X[N],Y[N],a[N];
void solve(){
    cin>>n>>m; For(i,1,n) cin>>X[i]>>Y[i];; For(i,1,n) cin>>a[i];
    // if(n<=100){
        ll ans=0;
        For(i,1,n) For(j,i+1,n) For(k,j+1,n){
            if(i%10==0&&j==i+1&&k==j+1) cerr<<"i="<<i<<'\n';
            int tt[3]={abs(X[i]-X[j])+abs(Y[i]-Y[j]),abs(X[i]-X[k])+abs(Y[i]-Y[k]),abs(X[j]-X[k])+abs(Y[j]-Y[k])};
            sort(tt,tt+3);
            if(O[tt[1]]) ans+=a[i]+a[j]+a[k];
        }
        cout<<ans<<'\n';
    // }
}
signed main(){
    // Fin("B2.in");
		Fin("chess.in"); Fout("chess.out");
    for(int i=2;i*i<=M-5;i++) for(int j=i*i;j<=M-5;j+=i) np[j]=1;
    For(i,2,M-5) if(!np[i]&&(i<=10||i%20==3)) O[i]=1;
    int T; cin>>T; while(T--) solve();
    cerr<<"Time = "<<clock()<<" ms"<<endl;
    return 0;
}

// START TYPING IF YOU DON'T KNOW WHAT TO DO
// STOP TYPING IF YOU DON'T KNOW WHAT YOU'RE DOING
// CONTINUE, NON-STOPPING, FOR CHARLIEVINNIE

// Started Coding On: June 03 Sat, 09 : 41 : 12